<?php
require_once("report.php");
class Summary_items extends Report
{
	function __construct()
	{
		parent::__construct();
	}
	
	public function getDataColumns()
	{
		return array(
                    array('data'=>lang('reports_barcode'), 'align'=> 'left'),
                    array('data'=>lang('reports_item'), 'align'=> 'left'),
                    array('data'=>lang('reports_description'), 'align'=> 'left'),
                    array('data'=>lang('reports_quantity_purchased'), 'align'=> 'left'),
                    array('data'=>lang('reports_sold'), 'align'=> 'left'),
                    array('data'=>lang('reports_stock'), 'align'=> 'left'),
                    array('data'=>lang('reports_unit_price'), 'align'=> 'left'),
                    array('data'=>lang('reports_sale_price'), 'align'=> 'left'),
                    array('data'=>lang('reports_subtotal'), 'align'=> 'right'),
                    array('data'=>lang('reports_profit'), 'align'=> 'right'),
                    array('data'=>lang('reports_suppliers'), 'align'=> 'left'), 
                    
                );
	}
        /*
         * function getDatas
         * @author: tran tuan
         * @email: trantuanckc@gmail.com
         */
        public function getDatas()
	{
		$this->db->select('name, sum(quantity_purchased) as sold, company_name as suppliers, (select sum(trans_inventory) from phppos_inventory where phppos_items.item_id=phppos_inventory.trans_items and trans_inventory > 0) as qty, quantity as stock, reorder_level,item_number as barcode,phppos_items.description, avg(item_unit_price) as sale_price, unit_price, ((unit_price * sum(quantity_purchased)) - (sum(quantity_purchased)* avg(item_unit_price))) as profits,sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(profit) as profit');
		$this->db->from('sales_items_temp');
		$this->db->join('items', 'sales_items_temp.item_id = items.item_id');
                $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id');
                
               
		if ($this->params['sale_type'] == 'sales')
		{
			$this->db->where('quantity_purchased > 0');
		}
		elseif ($this->params['sale_type'] == 'returns')
		{
			$this->db->where('quantity_purchased < 0');
                }
		if($this->params['category'] != '0'){
                    $this->db->where('items.category',$this->params['category']);
                }
                if($this->params['barcode'] != '' && $this->params['barcode'] != '0' ){
                    $this->db->where('items.item_number',$this->params['barcode']);
                }
                if($this->params['item'] != '' && $this->params['item'] != '0'){
                    $this->db->like('items.name',$this->params['item']);
                }
                if($this->params['serial_number'] != '' && $this->params['serial_number'] != '0'){
                    $this->db->where('items.serial_number',$this->params['serial_number']);
                }
                
                
		$this->db->where($this->db->dbprefix('sales_items_temp').'.deleted', 0);
		$this->db->group_by('items.item_id');
		$this->db->order_by('name');
                
		return $this->db->get()->result_array();		
	}
	
	public function getData()
	{
		$this->db->select('name, sum(quantity_purchased) as quantity_purchased, sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(profit) as profit');
		$this->db->from('sales_items_temp');
		$this->db->join('items', 'sales_items_temp.item_id = items.item_id');
		if ($this->params['sale_type'] == 'sales')
		{
			$this->db->where('quantity_purchased > 0');
		}
		elseif ($this->params['sale_type'] == 'returns')
		{
			$this->db->where('quantity_purchased < 0');
		}
                
		$this->db->where($this->db->dbprefix('sales_items_temp').'.deleted', 0);
		$this->db->group_by('items.item_id');
		$this->db->order_by('name');

		return $this->db->get()->result_array();		
	}
	
	public function getSummaryData()
	{
		$this->db->select('sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(profit) as profit');
		$this->db->from('sales_items_temp');
		$this->db->join('items', 'sales_items_temp.item_id = items.item_id');
		if ($this->params['sale_type'] == 'sales')
		{
			$this->db->where('quantity_purchased > 0');
		}
		elseif ($this->params['sale_type'] == 'returns')
		{
			$this->db->where('quantity_purchased < 0');
		}
		$this->db->where($this->db->dbprefix('sales_items_temp').'.deleted', 0);
		return $this->db->get()->row_array();
	}
}
?>